package com.vincent.kdp.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vincent.baseapp.base.BaseFragment;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.Activity.AddConsumeActivity;
import com.vincent.kdp.Activity.LoginActivity;
import com.vincent.kdp.Activity.ReserveActivity;
import com.vincent.kdp.Activity.ShopCartOkActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.adapter.HotCommoditiesAdapter;
import com.vincent.kdp.ui.adapter.ShopCartListAdapter;
import com.vincent.kdp.ui.bean.response.ShopCartListOut;
import com.vincent.kdp.ui.fragment.contract.ShopCartContract;
import com.vincent.kdp.ui.fragment.presenter.ShopCartPresenter;
import com.vincent.kdp.ui.other.MainActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/22 0022.
 */

public class ShopCartFragment extends BaseFragment<ShopCartContract.View, ShopCartPresenter> implements ShopCartContract.View {
    @Bind(R.id.shopcart_lv)
    ListView shopcartLv;
    @Bind(R.id.empty_list_view)
    TextView empty_list_view;
    @Bind(R.id.shopcart_cb_ln)
    LinearLayout shopcart_cb_ln;

    @Bind(R.id.shopcart_cb)
    CheckBox shopcartCb;

    @Bind(R.id.shopcart_delete)
    TextView shopcart_delete;
    @Bind(R.id.shopcart_clearall)
    TextView shopcartClearall;
    @Bind(R.id.shopcart_buy)
    TextView shopcartBuy;
    @Bind(R.id.shopcart_bot_ln)
    LinearLayout shopcart_bot_ln;

    private ShopCartListAdapter mAdapter;
    private String ids = "";
    private String reserveIds = "";

    public static List<ShopCartListOut.ShopCartBean> shopCartList = new ArrayList<>();


    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_shopcart;
    }

    @Override
    protected void initViewData() {
        mAdapter = new ShopCartListAdapter(getActivity(), shopCartList);
        shopcartLv.setAdapter(mAdapter);
        shopcartLv.setEmptyView(empty_list_view);
        presenter.reqShopCartList();
        mAdapter.setOnShoppingCartChangeListener(new ShopCartListAdapter.OnShoppingCartChangeListener() {
            @Override
            public void onDataChange(String selectMoney, String selectCount) {
            }

            @Override
            public void onSelectItem(boolean isSelectedAll) {
                shopcartCb.setChecked(isSelectedAll);
            }
        });

        CompoundButton.OnCheckedChangeListener checkListener = mAdapter.getCheckAdapterListener();
        if (checkListener != null) {
            //即使换了一个新的Adapter，也要将“全选事件”传递给adapter处理；shopcartCb
            shopcartCb.setOnCheckedChangeListener(mAdapter.getCheckAdapterListener());
        }


    }

    @Override
    public ShopCartPresenter initPresenter() {
        return new ShopCartPresenter();
    }

    @Override
    public void initShopCartList(final List<ShopCartListOut.ShopCartBean> ShopCartList) {

        if (ShopCartList.size() <= 0||ShopCartList==null||"".equals(ShopCartList)) {
            shopcart_bot_ln.setVisibility(View.GONE);
        } else {
            shopcart_bot_ln.setVisibility(View.VISIBLE);
        }
        shopCartList.clear();
        shopCartList.addAll(ShopCartList);
        mAdapter.notifyDataSetChanged();


    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {

        } else {
            presenter.reqShopCartList();




//            if (MainActivity.reload == 1) {
//                presenter.reqShopCartList();
//                MainActivity.reload = 0;
//            }
        }
    }

    @Override
    public void relist() {
        Toast.makeText(mContext, "删除成功", Toast.LENGTH_LONG).show();
        presenter.reqShopCartList();
    }

    public ShopCartFragment() {
        super();
    }

    @OnClick({R.id.shopcart_cb_ln, R.id.shopcart_clearall, R.id.shopcart_buy,R.id.shopcart_reserve, R.id.shopcart_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.shopcart_cb_ln:
                if (ShopCartListAdapter.LinCheck) {
                    shopcartCb.setChecked(false);
                } else {
                    shopcartCb.setChecked(true);
                }
                break;
            case R.id.shopcart_delete:
                ids="";

                for (int j = 0; j < shopCartList.size(); j++) {
                    if (Integer.parseInt(shopCartList.get(j).getStatus()) != 0) {
                        if (ids.equals("")) {
                            ids = shopCartList.get(j).getId();

                        } else {
                            ids = ids + "," + shopCartList.get(j).getId();
                        }
                    }
                }
                System.out.println("ids============" + ids);
                if (ids.equals("")) {
                    Toast.makeText(mContext, "请先选择条目", Toast.LENGTH_LONG).show();
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                    builder.setMessage("是否删除?");
                    builder.setTitle("提示");
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            presenter.dellist(ids);
                            ids = "";
                        }
                    });

                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.setCancelable(false);
                    builder.create().show();
                }
                break;
            case R.id.shopcart_clearall:


                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setMessage("是否清空");
                builder.setTitle("提示");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ids = "";
                        for (int j = 0; j < shopCartList.size(); j++) {
                            if (ids.equals("")) {
                                ids = shopCartList.get(j).getId();

                            } else {
                                ids = ids + "," + shopCartList.get(j).getId();
                            }
                        }

                        presenter.dellist(ids);
                        ids = "";
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setCancelable(false);
                builder.create().show();

                break;
            case R.id.shopcart_buy:



                ids="";
                for (int j = 0; j < shopCartList.size(); j++) {
                    if (Integer.parseInt(shopCartList.get(j).getStatus()) != 0) {
                        if (ids.equals("")) {
                            ids = shopCartList.get(j).getId();

                        } else {
                            ids = ids + "," + shopCartList.get(j).getId();
                        }
                    }
                }
                if (ids.equals("")) {
                    Toast.makeText(mContext, "请先选择条目", Toast.LENGTH_LONG).show();
                } else {
                    Intent it=new Intent(getActivity(), ShopCartOkActivity.class);
                    it.putExtra("ids", ids);
                    startActivity(it);
                }

                break;
            case R.id.shopcart_reserve:
                reserveIds="";
                for (int j = 0; j < shopCartList.size(); j++) {
                    if (Integer.parseInt(shopCartList.get(j).getStatus()) != 0) {
                        if (reserveIds.equals("")) {
                            reserveIds = shopCartList.get(j).getBarCode()+":"+shopCartList.get(j).getGoodsName();

                        } else {
                            reserveIds = reserveIds + "," + shopCartList.get(j).getBarCode()+":"+shopCartList.get(j).getGoodsName();
                        }
                    }
                }
                if (reserveIds.equals("")) {
                    Toast.makeText(mContext, "请先选择条目", Toast.LENGTH_LONG).show();
                } else {
                    Intent intent =new Intent(getActivity(), ReserveActivity.class);
                    intent.putExtra("barCode", reserveIds);
                    Log.d("ReserveActivity","reserveIds----"+reserveIds);
                   startActivity(intent);
                }



            break;
        }
    }
}
